<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/24
 * Time: 14:04
 */

namespace Home\Controller;

use Home\Common\Controller\CommonController;
use Think\Controller;

/**
 * Class GoodinfoController
 * @package Home\Controller
 * 商品详情处理
 */
class GoodinfoController  extends CommonController
{
    /**
     * 商品显示
     */
      public function  index(){
       $gid=I('get.gid');
          if(!$gid){
//             $this->
          }
          //查询该商品的数据
         $good=M('goods')
             ->join("sp_pic ON sp_goods.gid=sp_pic.gid")
             ->where("sp_goods.gid=$gid")
             ->select();
          $arr=[];
          foreach($good as $v){
             $arr[]=$v['pic'];
          }
          $good=$good[0];
          $good['pic']=$arr;
          $good['style']=json_decode($good['style'],true);
          $this->assign('good',$good);
          //超找该商品的属性
          $product=M('product')->where("gid=$gid")->select();
          $style=[];
          foreach($product as $k=>$v){
               $style[]=json_decode($v['style'],true);
          }
//          dd($style);

          //获得属性组成的数组
          $arr1=[];
          foreach($style as $k=>$v){
            foreach($v as $m=>$n){
                $arr1[$m][]=$n;
            }
          }

          foreach($arr1 as $k=>$v){
              $arr1[$k]=array_unique($v);
          }
//          dd($arr1);
          $this->assign('arr1',$arr1);
//          dd($product);
//          dd($good);die;
          $this->display();
      }
    //异步查询货品数据
    public function getAjaxpro(){
        if(IS_AJAX){
//             dd($_POST);
            $gid=$_POST['gid'];
           $style=json_encode($_POST['style'],JSON_UNESCAPED_UNICODE);
//            dd($style);
            $product=M('product')->where("gid=$gid")
                ->where("style='$style'")
                ->find();
            if(!$product){
                $this->ajaxReturn(['valid'=>0,'stock'=>'没有存货']);
            }else{
                $this->ajaxReturn(['valid'=>$product['pcid'],'stock'=>$product['stock']]);
            }
//            dd($product);
        }

    }
}